<?php
//商品晒单控制器
class PraiseAction extends PublicAction{
	public function index(){
		$article=D("Article");
		$rows=$article->select();
		//dump($rows);
	
	}
	//根据评价ID，获取商品晒单数据
	public function getCommentIdData(){
		$article=D("Article");
		$where=array(
				"comment_id"=>intval($_GET["id"]),
				"status"=>1
				);
		$rows=$article->where($where)->select();
		//dump($rows);
		
		if ($_GET["data_type"]=="html"){
			//返回HTML
			$this->disply();
		}elseif ($_GET[data_type]=="json_html"){
			//返回HTML
				
		}else{
			//返回纯JSON
				
			JSON($rows,"数据获取成功",1);
		}
	}
	
	//根据商ID，获取商品晒单数据
	public function getGoodsIdData(){
		$article=D("Article");
		$where=array(
				"goods_id"=>intval($_GET["id"]),
				"status"=>1
		);
		$rows=$article->where($where)->select();
		
		
		if ($_GET["data_type"]=="html"){
			//返回HTML
			$this->disply();
		}elseif ($_GET[data_type]=="json_html"){
			//返回HTML
			
		}else{
			//返回纯JSON
			
			JSON($data,"数据获取成功",1);
		}
	}
	
	//根据商品ID+用户ID，获取商品晒单数据
	public function getGoodsIdAndUserIdData(){
		if (empty($_GET["uid"]) || empty($_GET["gid"])) JSON(0,"参数错误",0);
		$article=D("Article");
		$where=array(
				"goods_id"=>intval($_GET["gid"]),
				"user_id"=>intval($_GET["uid"]),
				"status"=>1
		);
		$rows=$article->where($where)->select();
		$count=$article->where($where)->count();
		if ($_GET["data_type"]=="html"){
			//返回HTML
			$this->disply();
		}elseif ($_GET[data_type]=="json_html"){
			//返回HTML
	
		}else{
			//返回纯JSON
			
			JSON($rows,"数据获取成功",$count);
		}
	}
	//增加晒单
	public function addPraise(){
		if ($this->isLogin() && $_POST["user_id"]==$this->isLogin() ){
			$title=mysql_escape_string($_POST["title"]);
			$content=mysql_escape_string($_POST["content"]);
			$goods_id=intval(empty($_GET["goods_id"])?$_POST["goods_id"]:$_GET["goods_id"]);
			if (empty($title)) JSON(0,"请填写标题",0);
			if (empty($content)) JSON(0,"请填写内容",0);
			if (empty($goods_id)) JSON(0,"错误的商品ID",0);
			$user_id=intval($_POST["user_id"]);
			$img=mysql_escape_string($_POST["img"]);
			
			$article=D("Article");	
			$data=array(
					"title"=>$title,
					"content"=>$content,
					"add_time"=>time(),
					"user_id"=>$user_id,
					"goods_id"=>$goods_id,
					"comment_id"=>intval($_POST["comment_id"]),
					"status"=>1
					
					);
			if ($res=$article->add($data)){
				if (!empty($_POST["img"])){
					$img=rtrim($_POST["img"],"|");
					$imgArray=explode("|",$img);
					if (count($imgArray) && count($imgArray)<=4){
						$article_images=D("ArticleImages");	
						$where=array(
								"goods_id"=>$goods_id,
								"status"=>1
								);
						if ($_POST["comment_id"]){
							$where["comment_id"]=intval($_POST["comment_id"]);
							$where["_logic"]="or";
						}
						$rows=$article->where($where)->find();
						$article_images=D("ArticleImages");
						$data["article_id"]=$rows["aid"];
						
						//将图片数据插入数据 表
						foreach ($imgArray as $key => $value) {
							
							$data["image_path"]=$value;
							$article_images->add($data);
							
						}
					}
					
				}
				if (!empty($_GET["url"])){
					URLS(urldecode($_GET["url"]));
					exit(0);
				}
				//不需返回
				JSON($res,"增加成功",0);
				
			}else{
				JSON(0,"系统产生了一个错误，请稍后再试",0);
			}
			
		
		}else{
			JSON(0,"登录超时",0);
		}
		
	}
	//上传图片
	public function upload_image(){
		if ($this->isLogin()){
			if ($_GET["dom"]){
				echo '<script>document.domain = "'.$_GET["dom"].'";</script>';
			}
			//if (empty($_POST['upload_file'])) JSON(0,"no_pic",0);
			try {
				//转移文件
				//MovePublicFile(str_replace("thumb_210_", "", $_POST['user_head_img']),"images");
				
				//上传文件
				import("ORG.Net.UploadFile");
				
				$upload = new UploadFile();
				$upload->maxSize  = 3145728000 ;
				$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');
				$upload->savePath =  './Public/Uploads/image/';
				$upload->thumb=true;
				$upload->thumbMaxWidth="50,200";
				$upload->thumbMaxHeight="50,200";
				$upload->uploadReplace=false;
				
				if(!$upload->upload()) {
					JSON(0,$upload->getErrorMsg(),0);
				
				}else{// 上传成功 获取上传文件信息
					
					$info =  $upload->getUploadFileInfo();
					$info=$info[0];
					$savepath_file_m=C("URL").$info["savepath"].$info["savename"];
					$savepath_file_z=C("URL").$info["savepath"].$upload->thumbPrefix
					.$info["savename"];
					$array=array(
							"source_img"=>$savepath_file_m,
							"thumb_img"=>$savepath_file_z,
							"save_file_path"=>$info["savepath"].$info["savename"]
							);
					//dump($info);
					JSON($array,"上传成功",1);
				
				}
				
			} catch (Exception $e) {
				JSON(0,"error:".$e->getErrorMsg(),0);
			}
			
			
		}else{
			echo '<script>document.domain = "'.$_GET["dom"].'";</script>';
			JSON(0,"登录超时",0);
			
		}
	}
	//删除上传图片
	public function del_upload_image(){
		if ($this->isLogin()){
			if ($_GET["dom"]){
				echo '<script>document.domain = "'.$_GET["dom"].'";</script>';
			}
		}
		//*******************
		
		
	}
	
	
	
}